package webservice;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

public class RestClient {
	
	public enum RequestMethod{
		GET,
		POST
	}
	
	public int responseCode;
	public String message;
	public String response;
	
	public void execute(RequestMethod method, String url, 
			ArrayList<NameValuePair> params, ArrayList<NameValuePair> headers) throws Exception{
		
		switch(method){
		case GET:
		{
			String paramString = "";
			if(params != null){
				paramString += "?";
				for(NameValuePair p : params){
					String pString = p.getName() + "=" + URLEncoder.encode(p.getValue(), "UTF-8");
					if(paramString.length() > 1){
						paramString += "&" + pString;
					}
					else{
						paramString += pString;
					}
				}
			}
			HttpGet request = new HttpGet(url + paramString);
			if(headers != null){
				headers = addCommonHeaderField(headers);
				for(NameValuePair h : headers){
					request.addHeader(h.getName(), h.getValue());
				}
			}
			executeRequest(request, url);
			break;
		}
		case POST:
		{
			HttpPost request = new HttpPost(url);
			if(headers != null){
				headers = addCommonHeaderField(headers);
				for(NameValuePair h : headers){
					request.addHeader(h.getName(), h.getValue());
				}
			}
			if(params != null){
				request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
			}
			executeRequest(request, url);
			break;
		}
		
		}
		
	}
	
	private void executeRequest(HttpUriRequest request, String url){
		HttpClient client = new DefaultHttpClient();
		HttpResponse httpResponse;
		try{
			httpResponse = client.execute(request);
			responseCode = httpResponse.getStatusLine().getStatusCode();
			message = httpResponse.getStatusLine().getReasonPhrase();
			HttpEntity entity = httpResponse.getEntity();
			
			if(entity != null){
				InputStream input = entity.getContent();
				response = convertStreamToString(input);
				input.close();
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	
	private String convertStreamToString(InputStream input){
		BufferedReader reader = new BufferedReader(new InputStreamReader(input));
		StringBuilder sb = new StringBuilder();
		String line = null;
		try{
			while((line = reader.readLine()) != null){
				sb.append(line + "\n");
			}
			input.close();
		}
		catch(IOException ioe){
			ioe.printStackTrace();
		}
		return sb.toString();
	}
	
	private ArrayList<NameValuePair> addCommonHeaderField(ArrayList<NameValuePair> headerList){
		headerList.add(new BasicNameValuePair("Content-Type", "application/x-www-form-urlencoded"));
		return headerList;
	}

}
